What is twilio-sync?
The twilio-sync npm package provides a set of tools for synchronizing state across multiple devices and users in real-time. It is part of Twilio's suite of APIs and is designed to help developers build collaborative applications with ease.
What are twilio-sync's main functionalities?
Document
Documents are key-value stores that can be used to synchronize state across multiple clients. This example demonstrates how to create or access a document, listen for updates, and update the document.
const SyncClient = require('twilio-sync');
const client = new SyncClient('your_twilio_sync_token');
client.document('MyDocument').then((doc) => {
doc.on('updated', (data) => {
console.log('Document updated:', data);
});
return doc.update({ foo: 'bar' });
}).then((updatedDoc) => {
console.log('Updated document:', updatedDoc);
}).catch((error) => {
console.error('Error:', error);
});
List
Lists are ordered collections of items that can be used to synchronize lists of data across multiple clients. This example shows how to create or access a list, listen for new items, and add an item to the list.
const SyncClient = require('twilio-sync');
const client = new SyncClient('your_twilio_sync_token');
client.list('MyList').then((list) => {
list.on('itemAdded', (item) => {
console.log('Item added:', item);
});
return list.push({ foo: 'bar' });
}).then((item) => {
console.log('Added item:', item);
}).catch((error) => {
console.error('Error:', error);
});
Map
Maps are key-value stores where each key can have multiple fields. This example demonstrates how to create or access a map, listen for item updates, and set an item in the map.
const SyncClient = require('twilio-sync');
const client = new SyncClient('your_twilio_sync_token');
client.map('MyMap').then((map) => {
map.on('itemUpdated', (item) => {
console.log('Item updated:', item);
});
return map.set('key', { foo: 'bar' });
}).then((item) => {
console.log('Set item:', item);
}).catch((error) => {
console.error('Error:', error);
});
Stream
Streams are used to publish and subscribe to messages in real-time. This example shows how to create or access a stream, listen for new messages, and publish a message to the stream.
const SyncClient = require('twilio-sync');
const client = new SyncClient('your_twilio_sync_token');
client.stream('MyStream').then((stream) => {
stream.on('messagePublished', (message) => {
console.log('Message published:', message);
});
return stream.publishMessage({ foo: 'bar' });
}).then((message) => {
console.log('Published message:', message);
}).catch((error) => {
console.error('Error:', error);
});
Other packages similar to twilio-sync
firebase
Firebase is a comprehensive app development platform that includes real-time database capabilities. It allows for real-time synchronization of data across clients, similar to Twilio Sync. Firebase offers more extensive features such as authentication, hosting, and cloud functions, making it a more all-encompassing solution compared to Twilio Sync.
pusher
Pusher is a service that provides real-time APIs for building interactive applications. It offers channels for real-time communication, similar to Twilio Sync's streams. Pusher is known for its ease of use and quick setup, but it may not offer the same level of integration with other services as Twilio Sync.
ably
Ably is a real-time messaging service that provides pub/sub messaging, presence, and history features. It is similar to Twilio Sync in terms of real-time data synchronization but offers additional features like message history and presence tracking. Ably is designed to be highly scalable and reliable.
Twilio Sync JavaScript client library
Twilio Sync is Twilio's state synchronization service, offering two-way real-time communication between browsers, mobiles, and the cloud.
Visit our official site for more details: https://www.twilio.com/sync
Installation
via NPM
npm install --save twilio-sync
Using this method, you can require
twilio-sync.js like so:
const { SyncClient } = require('twilio-sync');
const syncClient = new SyncClient(token);
via CDN
Releases of twilio-sync.js are hosted on a CDN, and you can include these
directly in your web app using a <script> tag.
<script type="text/javascript" src="//sdk.twilio.com/js/sync/v3.2/twilio-sync.min.js"></script>
Using this method, twilio-sync.js will set a browser global:
const syncClient = new Twilio.Sync.Client(token);
Usage
To use the library, you need to generate an Access Token and pass it to the Sync Client constructor.
The Twilio SDK Starter applications for Node.js, Java, PHP, Ruby, Python, C# provide an easy way to set up a token generator locally.
Alternatively, you can set up a Twilio Function based on the Sync Access Token template.
// Obtain a JWT access token: https://www.twilio.com/docs/sync/identity-and-access-tokens
const token = '<your-access-token-here>';
const syncClient = new Twilio.Sync.Client(token);
// Open a Document by unique name and update its data
syncClient.document('MyDocument')
.then(function(document) {
// Listen to updates on the Document
document.on('updated', function(event) {
console.log('Received Document update event. New data:', event.data);
});
// Update the Document data
const newData = { temperature: 23 };
return document.set(newData);
})
.then(function(updateResult) {
console.log('The Document was successfully updated', updateResult)
})
.catch(function(error) {
console.error('Unexpected error', error)
});
For more code examples for Documents and other Sync objects, refer to the SDK API Docs.
Changelog
See this link.